<template>
    <div>
        <!--查询表单-->
        <div class="search-div">
            <el-form label-width="70px" size="small">
               
                <el-row>
                    <el-col :span="24" style="margin-left: 430px;">

                        <el-form-item label="角色名称">
                            <el-input style="width: 200px;margin-right: 10px;" v-model="searchObj.roleName"
                                placeholder="角色名称"></el-input>
                            <el-button type="primary" icon="el-icon-search" size="mini"
                                @click="fetchData()">搜索</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
                        </el-form-item>

                    </el-col>
                </el-row>

            </el-form>
        </div>

        <!-- 工具条 -->
        <div class="tools-div">
            <!-- <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button> -->
            <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="batchRemove()">批量删除</el-button>
        </div>

        <!-- :disabled="$hasBP('bnt.sysRole.add')  === false" -->



        <!-- 表格 -->
        <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" />

            <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column prop="roleName" label="角色名称" />
            <el-table-column prop="roleCode" label="角色编码" />
            <el-table-column prop="createTime" label="创建时间" width="160" />
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改" />
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)"
                        title="删除" />
                    <el-button type="warning" icon="el-icon-baseball" size="mini" @click="showAssignAuth(scope.row)"
                        title="分配权限" />
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-pagination :current-page="page" :total="total" :page-size="limit"
            style="padding: 30px 0; text-align: center;" layout="total, prev, pager, next, jumper"
            @current-change="fetchData" />

        <!-- 添加/修改角色信息的对话框 -->
        <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
            <el-form ref="dataForm" :model="sysRole" label-width="150px" size="small" style="padding-right: 40px;">
                <el-form-item label="角色名称">
                    <el-input v-model="sysRole.roleName" />
                </el-form-item>
                <el-form-item label="角色编码">
                    <el-input v-model="sysRole.roleCode" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
                <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
            </span>
        </el-dialog>

    </div>


</template>
<script>

import api from '@/api/role/role.js'
export default {
    data() {
        return {
            list: [],
            total: 0,
            page: 1,
            limit: 3,
            searchObj: {},
            listLoading: true, //显示加载转圈圈效果
            dialogVisible: false, //是否显示对话框，true：表示，false:不显示
            sysRole: {}, //封装角色信息的对象
            selectValueData: [] //选中的数据
        }
    },
    created() {
        this.fetchData();
    },
    methods: {
        fetchData(page = 1) {
            this.page = page;
            //调用后端的接口获得分页后的数据
            api.getPageList(this.page, this.limit, this.searchObj).then(response => {
                console.log(response); //response中封装了后端返回的数据结果
                //把返回的数据赋值给当前页面的变量
                this.list = response.data.records;
                this.total = response.data.total;
                this.listLoading = false; //禁用加载转圈圈效果
            })
        },
        edit(id) {
            this.dialogVisible = true;
            //去数据库中查询旧数据
            api.getRoleById(id).then(response => {
                this.sysRole = response.data;
            })
        },
        removeDataById(id) {
            //通过axios的工具类中方法，调用后台删除角色方法，返回结果
            api.removeRoleById(id).then(response => {
                this.$confirm('确认要删除吗?', '提示', {
                    confirmButtonText: '确定删除',
                    cancelButtonText: '取消删除',
                    type: 'warning'
                }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    // 刷新页面
                    this.fetchData();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            });
        },
        resetData() {
            this.searchObj = {}; //清空对象的数据
            //再次刷新列表数据
            this.fetchData();
        },
        add() {
            this.dialogVisible = true,
                this.sysRole = {}
        },
        saveOrUpdate() {
            if (this.sysRole.id != null) {
                this.updateRole(); //修改
            } else {
                this.addRole(); //添加
            }
        },
        addRole() {
            api.saveRole(this.sysRole).then(response => {
                //弹一下操作成功的提示框
                this.$message({
                    type: 'success',
                    message: '添加成功!'
                });
                this.dialogVisible = false; //隐藏对象框
                this.fetchData(); //刷新数据
            });
        },
        updateRole() {
            //同步数据到数据库当中
            api.updateRole(this.sysRole).then(response => {
                //弹一下操作成功的提示框
                this.$message({
                    type: 'success',
                    message: '修改成功!'
                });
                this.dialogVisible = false; //隐藏对象框
                this.fetchData(); //刷新数据
            });
        },
        batchRemove() {
            //判断有没有选中的数据
            if (this.selectValueData.length == 0) { //没有选中的数据
                this.$message.warning('请选择要删除的记录！');
                return; //让当前方法结束
            }
            this.$confirm('确认要删除吗?', '提示', {
                confirmButtonText: '确定删除',
                cancelButtonText: '取消删除',
                type: 'warning'
            }).then(() => {
                let ids = []; //声明了一个数组类型的变量，专门用来存储要删除的id值
                for (let i = 0; i < this.selectValueData.length; i++) {
                    let obj = this.selectValueData[i];
                    ids.push(obj.id);
                }
                //同步数据到数据库当中
                api.bactchremoveId(ids).then(response => {
                    //弹一下操作成功的提示框
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.fetchData(); //刷新数据
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //当选中表格第一列的复选框时会自动触发的事件
        handleSelectionChange(selectValue) {
            //console.log(selectValue);
            this.selectValueData = selectValue
        },
        showAssignAuth(row) {
            this.$router.push('/system/assignAuth?id=' + row.id + '&roleName=' + row.roleName);
        }
    }
}
</script>

<style lang="css" scoped></style>